package com.nikitova.craps3d.model 
{
	import com.carlcalderon.arthropod.Debug;
	import com.nikitova.craps3d.constants.AppData;
	import com.nikitova.craps3d.constants.Notifications;
	import com.nikitova.craps3d.model.vo.MaterialDataVO;
	import com.nikitova.utils.data.FileLoader;

	import org.puremvc.as3.multicore.utilities.startupmanager.interfaces.IStartupProxy;

	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.Dictionary;

	/**
	 * @author schranz
	 */
	public class MaterialsProxy extends EntityProxy implements IStartupProxy 
	{
		public static const NAME 		: String = "MaterialsProxy";
		public static const SRNAME		: String = 'MaterialsSRProxy';
		
		private var __groupNames : Dictionary;
		private var __swfPath : String;
		private var __fileLoader:FileLoader = new FileLoader();

		public function MaterialsProxy()
		{
			super(NAME);
			
			materialsdata = new Dictionary();
			createGroupNames();
		}
		
		public function load():void
		{
			var url : String = __swfPath + "materials.swf";	
			__fileLoader.addEventListener(FileLoader.FILE_LOADED, fileLoaded);
			__fileLoader.loadFile(url);
		}
		
		private function createGroupNames() : void 
		{
			__groupNames = new Dictionary();
			__groupNames[AppData.TABLE_TEXTURES] = new Vector.<String>();
			__groupNames[AppData.TABLE_TEXTURES].push(AppData.TABLE_CLOTH);
			__groupNames[AppData.DICE_TEXTURES] = new Vector.<String>();
			__groupNames[AppData.DICE_TEXTURES].push(AppData.DICE_ONE);
		}

		private function fileLoaded(event:Event) : void
		{
			var materialsMC : MovieClip = MovieClip(__fileLoader.fileData.content);
//			bundle movieclips in swf by body-group as specified in menu xml's
			for (var group : String in __groupNames) 
			{
				for (var id : String in __groupNames[group]) 
				{
					var materialMC : MovieClip = materialsMC[group+"_"+__groupNames[group][id]];
					if(materialMC != null)
					{
						materialMC.gotoAndStop(1);
						var materialData : MaterialDataVO = new MaterialDataVO();
						materialData.assetSwf = materialMC;
						var bmd:BitmapData = new BitmapData(materialMC.width,materialMC.height,true,0x000000);
						bmd.draw(materialMC);
						materialData.bmd = bmd;
						materialsdata[group+"_"+__groupNames[group][id]] = materialData;
					}
				
				}
			}
			sendLoadedNotification( Notifications.MATERIALS_LOADED, NAME, SRNAME );
		}

//		private function getClass( _modelId : Number, _className : String ) : Class 
//		{
//			var assetSwf : Loader = MaterialDataVO(materialsdata[_modelId]).assetSwf;
//			try 
//			{
//				return assetSwf.contentLoaderInfo.applicationDomain.getDefinition(_className) as Class;
//			} catch (e : Error) 
//			{
//				//		         	trace("ERROR : "+ _className + " definition not found in " + __target);
//				throw new IllegalOperationError(_className + " definition not found in " + __target);
//			}
//			return null;
//		}

//		public function getAssetByLinkageId( group : String, id : String ) : MaterialDataVO 
//		{
//			if( materialsdata[group][id] != null )
//			{
//				
//				return MaterialDataVO(materialsdata[group][id]);
//			}
//			return null;
//		}
		public function getMaterialByID( group : String, name : String ) : MaterialDataVO 
		{
			
			if( materialsdata[group+"_"+name] != null )
			{
				return MaterialDataVO(materialsdata[group+"_"+name]);
			}
			return null;
		}
		public function set swfPath(value : String) : void
		{
			__swfPath = value;
		}
		public function get materialsdata() : Dictionary 
		{
			return data as Dictionary ;
		}

		public function set materialsdata(_data : Dictionary ) : void
		{
			data = _data;
		}
	}
}
